#include "log.h"
#include <time.h>

void log_write(char* msg, int print_time, int flag) {
    FILE* file_ptr;
    if(flag == 0) {
        file_ptr = fopen("access_log.txt", "a+");
    }
    else {
        file_ptr = fopen("error_log.txt", "a+");
    }
    
    time_t now = time(0);
    struct tm tm = *gmtime(&now);
    char date_time[TYPE_LEN];
    strftime(date_time, TYPE_LEN, "%a, %d %b %Y %H:%M:%S %Z", &tm);

    char message[LOG_BUF_SIZE];

    if(file_ptr != NULL) {
        if(print_time == FALSE) {
            fputs(msg, file_ptr);
        } else {
            sprintf(message, "%s %s\r\n", date_time, msg);
            fputs(message, file_ptr);
        }
        fclose(file_ptr);
    }
}